package org.particlethink.mapper.impl;

import org.particlethink.config.RequestConfig;
import org.particlethink.context.Context;
import org.particlethink.mapper.MasterPlanCategoryMapper;
import org.particlethink.model.MasterPlanCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Repository
//@ConditionalOnMissingBean(DevMaserPlanCategoryMapper.class)
@ConditionalOnExpression("#{!'dev'.equals('${spring.profiles.active}')}")
public class ProMaserPlanCategoryMapper implements MasterPlanCategoryMapper {

    @Autowired
    private RequestConfig requestConfig;


    @Override
    public List<MasterPlanCategory> selectByIds(String token, List<String> ids) {
        return all(token).stream().filter(c -> ids.contains(c.getId())).collect(Collectors.toList());
    }

    @Override
    public MasterPlanCategory selectById(String token, String id) {
        return all(token).stream().filter(c -> c.getId().equals(id)).findFirst().orElse(null);
    }

    @Override
    public List<MasterPlanCategory> all(String token) {
        try {
            return DictRequestHelper.queryDictionaries(requestConfig.getUrl(), "总计划大类", token).stream().map(name -> {
                return new MasterPlanCategory(name, null, null, name);
            }).collect(Collectors.toList());
        } catch (URISyntaxException | IOException e) {
            throw new RuntimeException(e);
        }
    }
}
